package org.ehe.finance.voucher.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.finance.voucher.domain.bo.VoucherTypeCreateBo;
import org.ehe.finance.voucher.domain.bo.VoucherTypeQueryBo;
import org.ehe.finance.voucher.domain.bo.VoucherTypeUpdateBo;
import org.ehe.finance.voucher.domain.vo.ErpFinanceVoucherTypeVo;
import org.ehe.finance.voucher.service.ErpFinanceVoucherTypeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.List;
/**
 * 凭证类别管理
 * @author :zhangnn
 * @className :ErpFinanceVoucherTypeController
 * @description: TODO
 * @date 2025-08-22 17:59:21
 */
@RestController
@RequestMapping("/finance/voucher-type")
@RequiredArgsConstructor
@Validated
public class ErpFinanceVoucherTypeController extends BaseController {

    private final ErpFinanceVoucherTypeService voucherTypeService;

    /**
     * 分页查询凭证类别
     * @param bo
     * @param pageQuery
     * @return
     */
    @GetMapping("/page")
    @SaCheckPermission("finance:voucherType:list")
    public TableDataInfo<ErpFinanceVoucherTypeVo> page(@Valid VoucherTypeQueryBo bo, PageQuery pageQuery) {
        TableDataInfo<ErpFinanceVoucherTypeVo> erpFinanceVoucherTypeVoTableDataInfo = voucherTypeService.pageVoucherTypes(bo, pageQuery);
        return erpFinanceVoucherTypeVoTableDataInfo;
    }

    /**
     * 获取凭证类别详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @SaCheckPermission("finance:voucherType:query")
    public R<ErpFinanceVoucherTypeVo> getById(@PathVariable @NotNull Long id) {
        ErpFinanceVoucherTypeVo result = voucherTypeService.getVoucherTypeById(id);
        return R.ok(result);
    }

    /**
     * 创建凭证类别
     * @param createDTO
     * @return
     */
    @PostMapping
    @SaCheckPermission("finance:voucherType:add")
    public R<Long> create(@Valid @RequestBody VoucherTypeCreateBo createDTO) {
        Long id = voucherTypeService.createVoucherType(createDTO);
        return R.ok("创建成功", id);
    }

    /**
     * 更新凭证类别
     * @param updateDTO
     * @return
     */
    @Operation(summary = "更新凭证类别")
    @PostMapping("/edit")
    @SaCheckPermission("finance:voucherType:edit")
    public R<Void> update(@Valid @RequestBody VoucherTypeUpdateBo updateDTO) {
        voucherTypeService.updateVoucherType(updateDTO);
        return R.ok("更新成功");
    }

    /**
     * 删除凭证类别
     * @param id
     * @return
     */
    @Log(title = "删除凭证类别", linkType = "finance", businessType = BusinessType.INSERT, idField = "id", nameField = "id")
    @DeleteMapping("/{id}")
    @SaCheckPermission("finance:voucherType:remove")
    public R<Void> delete(@PathVariable @NotNull Long id) {
        voucherTypeService.deleteVoucherType(id);
        return R.ok("删除成功");
    }

    /**
     * 批量删除凭证类别
     * @param ids
     * @return
     */
    @Log(title = "批量删除凭证类别", linkType = "finance", businessType = BusinessType.INSERT, idField = "id", nameField = "id")
    @DeleteMapping("/batch")
    @SaCheckPermission("finance:voucherType:remove")
    public R batchDelete(@RequestBody @NotNull List<Long> ids) {
        voucherTypeService.batchDeleteVoucherTypes(ids);
        return R.ok("删除成功");
    }

    /**
     * 获取启用的凭证类别列表
     * @return
     */
    @GetMapping("/enabled")
    @SaCheckPermission("finance:voucherType:list")
    public R<List<ErpFinanceVoucherTypeVo>> getEnabledList() {
        List<ErpFinanceVoucherTypeVo> result = voucherTypeService.getEnabledVoucherTypes();
        return R.ok(result);
    }

    /**
     * 修改凭证类别状态
     * @param id
     * @param status
     * @return
     */
    @Log(title = "修改凭证类别状态", linkType = "finance", businessType = BusinessType.INSERT, idField = "id", nameField = "status")
    @PostMapping("/status/{id}/{status}")
    @SaCheckPermission("finance:voucherType:edit")
    public R<Void> changeStatus( @PathVariable @NotNull Long id,
                                 @PathVariable @NotNull Integer status) {
        voucherTypeService.changeStatus(id, status);
        return R.ok("修改状态成功");
    }

    /**
     * 检查编码是否存在
     * @param typeCode
     * @param excludeId
     * @return
     */
    @GetMapping("/check-code/{typeCode}")
    @SaCheckPermission("finance:voucherType:query")
    public R<Boolean> checkTypeCode(@PathVariable String typeCode,
                                    @RequestParam(required = false) Long excludeId) {
        boolean exists = voucherTypeService.checkTypeCodeExists(typeCode, excludeId);
        return R.ok(exists);
    }

    /**
     * 检查凭证字是否存在
     * @param prefix
     * @param excludeId
     * @return
     */
    @GetMapping("/check-prefix/{prefix}")
    @SaCheckPermission("hasPermission('finance:voucherType:query')")
    public R<Boolean> checkPrefix(@PathVariable String prefix,
                                  @RequestParam(required = false) Long excludeId) {
        boolean exists = voucherTypeService.checkPrefixExists(prefix, excludeId);
        return R.ok(exists);
    }

    /**
     * 生成凭证号
     * @param voucherTypeId
     * @param period
     * @return
     */
    @GetMapping("/generate-no/{voucherTypeId}/{period}")
    @SaCheckPermission("finance:voucher:add")
    public R generateVoucherNo(@PathVariable @NotNull Long voucherTypeId,@PathVariable String period) {
        String voucherNo = voucherTypeService.generateVoucherNo(voucherTypeId, period);
        return R.ok(voucherNo);
    }
}
